function image_compare(imgfile)

persistent linesty
% 
image_file = [imgfile '.mat'];
if(exist(image_file) ~= 2) % file does not seem to exist
   msgbox(sprintf('Image File %s not found', image_file));
   return;
end;

ix=load([imgfile '.mat']);

[draw, ndraw] = image_drawread(imgfile, 'ALL'); % read the drawings associated with the file (there may be none)

ix = ix.save_data;
[tw, sr] = image_times; % get image and spike times
if(isempty(tw)) % fake it... don't have third channel to work withh.
   sr = 7.5;
   tw = [0:sr:(length(ix)-2)*sr];
end;
tw = tw(2:end);
%tw = tw - 0.5*sr;
x=ix(:,[2:end]); % strip off the first column, which is just the index
x = x([3:end], :); % first entry is just a number; first real image is bad so start at 3.
sx = size(x);
for i = 1:sx(2)
   x(:,i)=x(:,i)-mean(x([1:3], i)); % just offset the baseline
end;
%xt = sr*[0:length(x)-1]/1000;
if(length(tw) < length(x))
   tw = [0 tw]; % missing pulse at beginning, sometimes happens for short integrations
end;
if(length(tw) < length(x)) % still? maybe took more image data than electrophys. Electrophys is limiting here.
   x = x([1:length(tw)],:); % make it match.
end;

df = datac('getdfile');
[v, t] = datac('getv');
v = v - 12; % correct for junction potential here.
[curr, t] = datac('geti');
curr = curr ./1000; % convert to nA
% get spike times for comparison.
if(isempty(v))
   return;
end;
[first_spike, first_isi, nr_spikes, spike_train] = find_spikes(df, 0, max(t), 0);

xx =  x; % spline(tw, x, t);

[xm, alljm] = max(xx); % find max for each trace.
[mxm, i] = max(xm); % find which of those is the max
jm = alljm(i) + 2; % find max image position.
tiff_file = [imgfile '.tif'];
if(exist(tiff_file) ~= 2) % file does not seem to exist
   QueMessage(sprintf('Corresponding Tiff File %s not found', tiff_file), 1);
else
   A = imread(tiff_file, 'tiff', jm); % read image from index
   imlist = [2:5];
   C = double(imread(tiff_file, 'tiff', imlist(1))); % compute an average for the baseline...
   for i = 2:length(imlist)
      C = C + double(imread(tiff_file, 'tiff', imlist(i))); % read the first valid image also
   end;
   B = C ./ length(imlist);
end;

% prepare display
h = findobj('Tag', 'image_compare');
if(isempty(h))
   return; % require the base window to be UP.
   %   h = figure;
   %   set(h, 'Tag', 'image_compare');
   %   clf;
end;
figure(h);
accflag = 0;
lslist = {'-' '--' ':' '-.' };
set(h, 'PaperOrientation', 'Landscape');
   msize = 3;
   msg_pos = [0.84 0.92 0.15 0.07];
   if(~exist('linesty'))
      linesty = 1;
   end;
   ha = findobj('Tag', 'accumulate');
if(strmatch(get(ha, 'Check'), 'off', 'exact')) % cause axes to be cleared.
   hax = findobj(h, 'Type', 'axes');
   delete(hax);
   linesty = 1; % reset line style on a per plot basis.
else
   accflag = 1;
end;
   % make tag names for current, voltage and fluorescence plots
   % tags are: filename_V/I/F_record
   [p, tagfn, e] = fileparts(df.filename);
   rn = df.record(1);
   tag_v = sprintf('%s_V_%03d', tagfn, rn)
   tag_i = sprintf('%s_I_%03d', tagfn, rn)
   tag_f = sprintf('%s_F_%03d', tagfn, rn)
   
   % clf;
h_text = findobj('Tag', 'Imagetitle');
if(isempty(h_text))
   subplot('Position', [0.25 0.90 0.5 0.09]);
   axis([0, 1, 0, 1]);axis('off');
   h_text = text(0.5, 0.5, 'I am the walrus ');
   set(h_text, 'FontName', 'Helvetica', 'FontSize', 8, 'HorizontalAlignment', 'center');
   set(h_text, 'Tag', 'Imagetitle');
end;
[i_path, i_file, i_ext] = fileparts(imgfile);
txt{1} = sprintf('Data File: %s, R=%d  Image File: %s', df.filename, df.record, [i_file i_ext]);
txt{2} = sprintf('%s ', datestr(now, 0));
set(h_text, 'String', txt, 'Color', 'black');

subplot('position', [0.4, 0.5, 0.55, 0.35]);
hp = plot(tw, xx, '-', 'LineWidth', 1.25, 'linestyle', lslist{linesty});
co = get(gca, 'ColorOrder');
set(hp, 'tag', tag_f);
u = get(gca, 'XLim');
set(gca, 'Xcolor', [0.25 0.25 0.25], 'Ycolor', [0.25, 0.25, 0.25]);
set(gca, 'XLim', [0 max(tw)]);
set(gca, 'FontSize', 7);
ylabel('\DeltaF/F');
set(gca, 'Box', 'off');
%grid;
hold on;
plot([0, max(tw)], [0, 0], '-', 'color', 'black', 'linestyle', '--', 'linewidth', 0.5);
%plot(tw, x, '^', 'MarkerFaceColor', 'black', 'MarkerEdgeColor', 'black', ...
%   'MarkerSize', 2.5);
%set(gca, 'YLim', [-10, 50]);
for i = 1:sx(2)
   mxp = max(tw);
   text(mxp*0.95, x(end, i)+0.5, sprintf(' %d', i), 'Fontsize', 7, 'color', co(mod(i-1, size(co, 1)) + 1,:));
end;

[yl] = get(gca, 'YLim');
if(~isempty(spike_train))
   for i = 1:length([spike_train.latency])
      st = spike_train(1).latency(i);
      st = [st st];
      plot(st, yl, '-r' );
   end;
end;
   datac_setcrosshair(gca, 'Image_info', 'ms', 'DF', msg_pos);


subplot('position', [0.4, 0.21, 0.55, 0.25]);
hold on;
hp=plot(t, v, 'Linewidth', 1, 'Linestyle', lslist{linesty});
linesty = linesty + 1; % now increment style.
if(linesty > length(lslist))
   linesty = 1; % modulus.
end;

set(hp, 'tag', tag_v);
set(gca, 'XLim', [0 max(tw)]);
set(gca, 'YLim', [-100 50]);
set(gca, 'FontSize', 7);
ylabel('mV');
%grid;
set(gca, 'Xcolor', [0.25 0.25 0.25], 'Ycolor', [0.25, 0.25, 0.25]);
   datac_setcrosshair(gca, 'Image_info', 'ms', 'mV', msg_pos);
   
   subplot('position', [0.4, 0.05, 0.55, 0.10]);
hold on;
hp = plot(t, curr, 'Linewidth', 1);
set(hp, 'tag', tag_i);
set(gca, 'XLim', u);
set(gca, 'XLim', [0 max(tw)]);
set(gca, 'YLim', [-1 1]);
set(gca, 'FontSize', 7);
ylabel('nA');
xlabel('ms');
%grid
set(gca, 'Xcolor', [0.25 0.25 0.25], 'Ycolor', [0.25, 0.25, 0.25]);
   datac_setcrosshair(gca, 'Image_info', 'ms', 'nA', msg_pos);


% stop here if we are "accumulating". 
ha = findobj('Tag', 'accumulate');
if(strmatch(get(ha, 'Check'), 'on', 'exact')) % cause axes to be cleared.
   return;
end;

% generate a colormap of type "lucifer"
% basically, red goes from 0 to sat (1) over range
% 				green goes from 0 to sat(1) over range
%				blue is always 0.

lucifer = [];
for i = 1:256
   if(i < 96) 
      red = 0;
   else
      red = (i - 96) * 4/3;
   end;
   if(red > 255) red = 255; end;
   if(i < 128)
      green = i * 2;
   else
      green = 255;
   end;
   if(i < 192)
      blue = 0;
   else
      blue = (i - 192) * 4;
   end;
   if(blue > 255) blue = 255; end;
   lucifer = [lucifer; [red green blue]/256];
end;


% the max image...
% only display images in non-accumulation mode.

subplot('position', [0.01, 0.1, 0.35, 0.35]);

A = 100*(double(A) - double(B))./double(B); % subtract the resting state from the image. - get delta F/F
low = min(min(A));
high = max(max(A));
imagesc(A); 
colormap(lucifer);
set(gca, 'Visible', 'off');
set(gca, 'dataaspectratiomode', 'manual');
set(gca, 'dataaspectratio', [1 1 1]);
ht = get(gca, 'Title');
%ti{1} = 'Maximum \DeltaF/F_0';
%ti{2} = sprintf('Image # %d at t = %6.1f', jm, tw(jm-2));
%set(ht, 'String', ti, 'Visible', 'on', 'FontSize', 8);
%hc = colorbar('horiz');
%set(hc, 'xaxislocation', 'bottom', 'fontsize', 6);
%set(get(hc, 'Title'), 'String', '\DeltaF/F_0', 'Visible', 'on', 'Fontsize', 6);
low = min(min(A));
high = max(max(A));

% superimpose the zone information (if any) on the plot.
if(~isempty(draw))
   for i = 1: length(draw)
      hl = line(lpath(draw(i).x, 'x'), lpath(draw(i).y, 'y'));
      set(hl, 'LineWidth', 0.75, 'Color', 'red');
      text(max(draw(i).x)+0.5, min(draw(i).y)+0.5, sprintf('%d', i), 'fontsize', 5, 'color', 'red');
   end;
end;
   datac_setcrosshair(gca, 'Image_info', 'X', 'Y', msg_pos);

% and with background subtractions.
subplot('position', [0.01, 0.55, 0.35, 0.35]);
%B = double(A) - double(B);
imagesc(B);
set(gca, 'Visible', 'off');
set(gca, 'dataaspectratiomode', 'manual');
set(gca, 'dataaspectratio', [1 1 1]);
ht = get(gca, 'Title');
%set(ht, 'String', 'Resting', 'Visible', 'on', 'FontSize', 8);
%hc = colorbar('horiz');
%set(hc, 'xaxislocation', 'bottom', 'fontsize', 6);
%set(get(hc, 'Title'), 'String', 'Rel. Units', 'Visible', 'on', 'Fontsize', 6);

   datac_setcrosshair(gca, 'Image_info', 'X', 'Y', msg_pos);

return;

function [p] = lpath(x, mode)
% take x and make the path needed to draw a rectangle.
if(length(x) ~= 2)
   p = [];
   return;
end;

switch mode
   case 'x'
      x(1) = x(1) -0.5;
      x(2) = x(2) + 0.5;
      p = [x(1) x(1) x(2) x(2) x(1)];
case 'y'
   x(1) = x(1) - 0.5;
   x(2) = x(2) + 0.5;
   p = [x(1) x(2) x(2) x(1) x(1)];
otherwise
   p = [];
end;

return;
